/**
 * Copyright (C) 2010 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2010 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.book.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.Observable;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JTextArea;

import com.kingcore.director.base.ui.DirectorPanel;
import com.kingcore.director.base.ui.DirectorToolBar;
import com.kingcore.director.main.MainFrame;

/**
 * <p>java class file describe</p>
 * @author Zeven on 2010-8-30
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class NoteBookMainPanel extends DirectorPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public NoteBookMainPanel(MainFrame baseFrame) {
		super(baseFrame);
		initializeInterface();

		constructAfter();
	}

	/**
	 * <p>java method describe</p>
	 * @author Zeven on 2010-8-30
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	private void initializeInterface(){
        this.setLayout(new BorderLayout());

		this.add( getToolBar(true), BorderLayout.NORTH);
        this.add( getDesktopPane(), BorderLayout.CENTER );
		this.add( getStatusBar(true), BorderLayout.SOUTH); 
//        this.add( getCreateNewButton(), BorderLayout.SOUTH ); 
        
		
		JInternalFrame internalFrame = createNewInternalFrame();
		try {
			internalFrame.setMaximum(true);
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}
		this.update(null, null);
	}

	private JDesktopPane desktopPane = null;
	private JDesktopPane getDesktopPane() {
		if (desktopPane==null) {
	        desktopPane = new JDesktopPane(); 
		}
		return desktopPane;
	}

//	private JButton getCreateNew = null;
//	private JButton getCreateNewButton() {
//		if (getCreateNew ==null ) {
//			getCreateNew = new JButton("创建新文章");
//			getCreateNew.addActionListener(this);//当用户按下按钮时，将运行actionPerformed()中的程序
//		}
//		return getCreateNew;
//	}

	private int count = 0;
	private int offset = 0;
	public void actionPerformed( ActionEvent e ){
		super.actionPerformed(e);
		
		Object obj = e.getSource();
		if (obj==this.toolBar.tb_creatNote) {
			this.createNewInternalFrame();
		}
	}

	 
	private JInternalFrame createNewInternalFrame() {
		JInternalFrame internalFrame = new JInternalFrame(
                "Noname "+(count++), true, true, true, true);  

	   internalFrame.setLocation( offset*25, offset*25);
	   internalFrame.setSize(680,500); 
	   internalFrame.setVisible(true);
	   offset++;
	   offset = offset>8?0:offset; //重新从左上角开始
	   //取得JInternalFrame的Content Pane,用以加入新的组件。
	   Container icontentPane = internalFrame.getContentPane();
	   JTextArea textArea = new JTextArea();
	   final JInternalFrame f_internalFrame = internalFrame;
	   
	   /*将JTextArea与JButton对象加入JInternalFrame中。由此呆知，JInteranlFrame加入组件
	    *的方式与JFrame是一模一样。
	    */
	   icontentPane.add(textArea,"Center");

//	   JButton closeBtn = new JButton("关闭");
//	   closeBtn.addActionListener(new ActionListener(){
//	   	public void actionPerformed( ActionEvent e ){
//	   		f_internalFrame.setVisible(false);
//	   		f_internalFrame.dispose();
//	   	}
//	   });
//	   icontentPane.add(closeBtn,"South");
	   //将JInternalFrame加入JDesktopPane中，如此一来，即使产生很多JInternalFrame,JDesktopPane也
	   //能将它们之间的关系管理得相当良好。
	   desktopPane.add(internalFrame);  
	   
	   try {
	       internalFrame.setSelected(true);
	   } catch (java.beans.PropertyVetoException ex) {
	     System.out.println("Exception while selecting");
	   }
	   return internalFrame;
	}

	/**
	 * 默认都使用CalendarToolBar，可以根据需要定制。
	 * @return
	 */
	public DirectorToolBar createToolBar(){
		if (this.toolBar==null) {
			this.toolBar = new NoteBookToolBar( this );
			//this.setPreferredSize(new Dimension(20,25));
		}
		return this.toolBar; 
	}
	
	@Override
	public void setToolBarVisible(boolean isToolBarShow) {
		if (isToolBarShow) {
			this.add( getToolBar(), BorderLayout.NORTH);
		}else{
			this.remove( getToolBar() );
		}
	} 
	
	@Override
	public boolean fetchData() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean saveData() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void refreshView() {
		super.refreshView();
		
		setStatus(StatusBar_Index_One, currentStatusInfor);
		
	}

	@Override
	public void update(Observable o, Object arg) {

		super.update(o, arg);
	}

}
